//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.persistence.entity.data.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.impl.TaskQueryImpl;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.persistence.CachedEntityMatcher;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityImpl;
import org.activiti.engine.impl.persistence.entity.data.AbstractDataManager;
import org.activiti.engine.impl.persistence.entity.data.TaskDataManager;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.TasksByExecutionIdMatcher;
import org.activiti.engine.task.Task;

public class MybatisTaskDataManager extends AbstractDataManager<TaskEntity> implements TaskDataManager {
  protected CachedEntityMatcher<TaskEntity> tasksByExecutionIdMatcher = new TasksByExecutionIdMatcher();

  public MybatisTaskDataManager(ProcessEngineConfigurationImpl processEngineConfiguration) {
    super(processEngineConfiguration);
  }

  public Class<? extends TaskEntity> getManagedEntityClass() {
    return TaskEntityImpl.class;
  }

  public TaskEntity create() {
    return new TaskEntityImpl();
  }

  public List<TaskEntity> findTasksByExecutionId(String executionId) {
    return this.getList("selectTasksByExecutionId", executionId, this.tasksByExecutionIdMatcher, true);
  }

  public List<TaskEntity> findTasksByProcessInstanceId(String processInstanceId) {
    return this.getDbSqlSession().selectList("selectTasksByProcessInstanceId", processInstanceId);
  }

  @Override
  public List<TaskEntity> findTasksByExecutionIdAll(String executionId) {
    return null;
  }

  public List<Task> findTasksByQueryCriteria(TaskQueryImpl taskQuery) {
    String query = "selectTaskByQueryCriteria";
    return this.getDbSqlSession().selectList("selectTaskByQueryCriteria", taskQuery);
  }

  public List<Task> findTasksAndVariablesByQueryCriteria(TaskQueryImpl taskQuery) {
    String query = "selectTaskWithVariablesByQueryCriteria";
    if (taskQuery.getFirstResult() >= 0 && taskQuery.getMaxResults() > 0) {
      int firstResult = taskQuery.getFirstResult();
      int maxResults = taskQuery.getMaxResults();
      if (taskQuery.getTaskVariablesLimit() != null) {
        taskQuery.setMaxResults(taskQuery.getTaskVariablesLimit());
      } else {
        taskQuery.setMaxResults(this.getProcessEngineConfiguration().getTaskQueryLimit());
      }

      taskQuery.setFirstResult(0);
      List<Task> instanceList = this.getDbSqlSession().selectListWithRawParameterWithoutFilter("selectTaskWithVariablesByQueryCriteria", taskQuery, taskQuery.getFirstResult(), taskQuery.getMaxResults());
      if (instanceList != null && !instanceList.isEmpty()) {
        int toIndex;
        if (firstResult > 0) {
          if (firstResult <= instanceList.size()) {
            toIndex = firstResult + Math.min(maxResults, instanceList.size() - firstResult);
            return instanceList.subList(firstResult, toIndex);
          } else {
            return Collections.EMPTY_LIST;
          }
        } else {
          toIndex = Math.min(maxResults, instanceList.size());
          return instanceList.subList(0, toIndex);
        }
      } else {
        return Collections.EMPTY_LIST;
      }
    } else {
      return Collections.EMPTY_LIST;
    }
  }

  public long findTaskCountByQueryCriteria(TaskQueryImpl taskQuery) {
    return (Long)this.getDbSqlSession().selectOne("selectTaskCountByQueryCriteria", taskQuery);
  }

  public List<Task> findTasksByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
    return this.getDbSqlSession().selectListWithRawParameter("selectTaskByNativeQuery", parameterMap, firstResult, maxResults);
  }

  public long findTaskCountByNativeQuery(Map<String, Object> parameterMap) {
    return (Long)this.getDbSqlSession().selectOne("selectTaskCountByNativeQuery", parameterMap);
  }

  public List<Task> findTasksByParentTaskId(String parentTaskId) {
    return this.getDbSqlSession().selectList("selectTasksByParentTaskId", parentTaskId);
  }

  public void updateTaskTenantIdForDeployment(String deploymentId, String newTenantId) {
    HashMap<String, Object> params = new HashMap();
    params.put("deploymentId", deploymentId);
    params.put("tenantId", newTenantId);
    this.getDbSqlSession().update("updateTaskTenantIdForDeployment", params);
  }
}
